package com.lc.rongzecaifu.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.adapter.BorrowingAlreadyDetailAdapter;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonBorrowHuankuanLijidetail;
import com.lc.rongzecaifu.conn.JsonBorrowHuankuandetail;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.lc.rongzecaifu.view.SimpleDividerItemDecoration;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
public class HuankuanDetailActivity extends BaseActivity implements View.OnClickListener {


    private String id;

    private RecyclerView.LayoutManager mLayoutManager;

    RecyclerView rv_01;

    public boolean canLoadMore = true;

    private RecyclerView.Adapter mAdapter;
    private List<String> mDatas;
    private List<JsonBorrowHuankuandetail.Info.Repayment_list> repayment_lists = new ArrayList<>();
    private ImageView iv_back;

    private String getId;
    String item_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huankuandetail);
        //  View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup)..inflate(R.layout.activity_huankuandetail, null)));

        initData2();
        rv_01 = (RecyclerView) findViewById(R.id.rv_01);
        rv_01.setLayoutManager(new LinearLayoutManager(this));
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        getAccessToken();
        getId = getIntent().getStringExtra("id");
        //  mAdapter = new BorrowingHuankuanAdapter(this,repayment_lists);
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.line_divider3);
        rv_01.addItemDecoration(new SimpleDividerItemDecoration(this, drawable, 30));
        mAdapter = new BorrowingAlreadyDetailAdapter(this, repayment_lists, this);

        rv_01.setAdapter(mAdapter);
        // id=getIntent().getStringExtra("id");


    }

    protected void initData2() {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
    }


    private void getAccessToken() {
        new JsonAccessToken("user/borrow_in", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
               /* LovelyToast.makeText(HuankuanDetailActivity.this, "请求成功！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
                initData(info.access_token);


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //   UtilToast.show(InvestManageDetailActivity.this, "安全验证失败！");
                LovelyToast.makeText(HuankuanDetailActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

            }
        }).execute(this, true);

    }


    private void initData(String access_token) {

        new JsonBorrowHuankuandetail(access_token, MyApplication.myPreferences.readUid(), getId,

                new AsyCallBack<JsonBorrowHuankuandetail.Info>() {

                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, JsonBorrowHuankuandetail.Info info) throws Exception {
                        repayment_lists.clear();
                        repayment_lists.addAll(info.repayment_list);


                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        //   UtilToast.show(InvestManageDetailActivity.this, JsonTendout.TOAST);
                        LovelyToast.makeText(HuankuanDetailActivity.this, "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);

                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {

                        canLoadMore = true;
                        mAdapter.notifyDataSetChanged();
                    }

                }).execute(HuankuanDetailActivity.this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.already_huan:

                Integer position2 = (Integer) view.getTag();
                JsonBorrowHuankuandetail.Info.Repayment_list listContent2 = repayment_lists.get(position2);
                item_id = listContent2.sort_order;
                getAccessToken2();
                //  getAccessToken();
                break;
        }
    }


    private void getAccessToken2() {
        new JsonAccessToken("user/borrow_repayment", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
               /* LovelyToast.makeText(HuankuanDetailActivity.this, "请求成功！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/
                obtainData(info.access_token);


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //   UtilToast.show(InvestManageDetailActivity.this, "安全验证失败！");
                LovelyToast.makeText(HuankuanDetailActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

            }
        }).execute(this, true);

    }


    private void obtainData(String access_token) {

        new JsonBorrowHuankuanLijidetail(access_token, MyApplication.myPreferences.readUid(), getId, item_id,

                new AsyCallBack<JsonBorrowHuankuanLijidetail.Info>() {

                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, JsonBorrowHuankuanLijidetail.Info info) throws Exception {
                        getAccessToken();
                        LovelyToast.makeText(HuankuanDetailActivity.this, JsonBorrowHuankuandetail.TOAST, 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

              /*      @Override
                    public void onSuccess(String toast, int type, JsonBorrowHuankuandetail.Info info) throws Exception {

                        repayment_lists.addAll(info.repayment_list);


                    }*/

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        //   UtilToast.show(InvestManageDetailActivity.this, JsonTendout.TOAST);
                        LovelyToast.makeText(HuankuanDetailActivity.this, "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);

                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {

                        canLoadMore = true;
                        mAdapter.notifyDataSetChanged();
                    }

                }).execute(HuankuanDetailActivity.this);
    }

}
